package ch.ethz.fcl.metrobuzz.render.od.flow_view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

import ch.ethz.fcl.metrobuzz.data.od.SelectLinkQuery;
import ch.ethz.fcl.metrobuzz.render.od.flow_view.interactions.ODMouseListener;
import ch.ethz.fcl.metrobuzz.render.od.flow_view.interactions.ODPopupMenu;
import ch.ethz.fcl.metrobuzz.render.od.flow_view.sankey.ODSankeyDiagramData;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Rectangle;

public class ODFrame extends JFrame {
	private static final long serialVersionUID = 557600852340181095L;

	private int width = 1440, height = 900;
	// private int width = 1680, height = 1050;
	private Rectangle sdBound = null;

	private ODComponent sdComponent;

	private ODSankeyDiagramData data;

	public int titleHeight = 22;

	public ODFrame(SelectLinkQuery slq) {
		super("OD Visualization");

		if (slq != null)
			data = new ODSankeyDiagramData(slq);

		this.setSize(width, height);
		this.setLocation(1920, 0);

		super.setUndecorated(false);

		if (!super.isUndecorated())
			sdBound = new Rectangle(160, 25, width - 160, height - 15);
		else
			sdBound = new Rectangle(160, 25, width - 160, height);

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		sdComponent = new ODComponent(slq, data, sdBound, width,
				height);

		this.add(sdComponent);

		this.pack();
		this.setVisible(true);

		ODMouseListener odsdml = new ODMouseListener(this,
				sdComponent.getData());
		sdComponent.addMouseListener(odsdml);
		sdComponent.addMouseMotionListener(odsdml);

		new ODPopupMenu(this, data);

		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					System.exit(0);
				}
			}
		});
	}

	public void update() {
		sdComponent.update();

		repaint();
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		getContentPane().setBackground(Color.WHITE);
		sdComponent.repaint();
	}

	public ODComponent getComponent() {
		return sdComponent;
	}

	public ODSankeyDiagramData getData() {
		return data;
	}

	public static void main(String[] args) {
		new ODFrame(null);
	}
}
